﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
/// <summary>
///CustomGrid 的摘要说明
/// </summary>
namespace yncTechWeb.Controls
{
    public enum ValidationType
    {
        None,
        Date,
        Number,
        Digit,
        Email,    
        URL,
        Regex
    }
    public class yncTextBox:TextBox
    {
        const string RequrieText = "&nbsp;<font color='Red'>*</font>";
        public struct ValidateEntity
        {
            public string Name;
            public int Max;
            public int Min;
        }
        public yncTextBox():base()
        {
            Required = false;
            ValidateType = ValidationType.None;

            //当验证数值是有效
            MaxVlaue = null;
            MinValue = null;

            MaxValueLength = -1;
            MinValueLength = -1;

            RegexMessage = string.Empty;
            RegexPattern = string.Empty;

            EqualToField = string.Empty;
            EqualToFiledText = string.Empty;
            this.CssClass = "textBox";

        }
        string fieldText = string.Empty;
        /// <summary>
        /// 在验证信息中出现的名称
        /// 如：“用户名”不为能空。
        /// </summary>
        public string FieldText
        {
            get
            {
                return fieldText;
            }
            set
            {
                fieldText = value;
            }

        }

        public ValidateEntity MaxEntity
        {
            get;
            set;
        }

        public ValidationType ValidateType
        {
            get;
            set;
        }

        public string EqualToField
        {
            get;
            set;
        }

        public string EqualToFiledText
        {
            get;
            set;
        }

        public int IntValue
        {
            get
            {
                if (string.IsNullOrEmpty(Text))
                {
                    return -1;
                }
                return int.Parse(Text);
            }
        }

        public float FloatValue
        {
            get
            {
                if (string.IsNullOrEmpty(Text))
                {
                    return 0;
                }
                return float.Parse(Text);
            }
        }

        public decimal DecimalValue
        {
            get
            {
                if (string.IsNullOrEmpty(Text))
                {
                    return -1;
                }
                return decimal.Parse(Text);
            }
        }
        
        public bool Required
        {
            get;
            set;
        }        
 
        public string MaxVlaue
        {
            get;
            set;
        }

        public string MinValue
        {
            get;
            set;
        }

        public int? MaxValueLength
        {
            get;
            set;
        }

        public int? MinValueLength
        {
            get;
            set; 
        }

       
        public string RegexMessage
        {
            get;
            set;
        }
        public string RegexPattern
        {
            get;
            set;
        }

        protected override void OnPreRender(EventArgs e)
        {
         

            bool needValidate = false;
            //必填验证
            if (Required)
            {
                needValidate = true;
                this.Attributes.Add("data-val-required", FieldText + " 必填字段。");
            }

            //数据类别验证
            if (ValidateType != ValidationType.None)
            {
                needValidate = true;
                switch (ValidateType)
                {
                    case ValidationType.Date:
                        this.Attributes.Add("data-val-date", "请输入正确格式的日期。");
                        break;
                    case ValidationType.Number:
                        this.Attributes.Add("data-val-number", "请输入合法的数字。");
                        break;
                    case ValidationType.Digit:
                        this.Attributes.Add("data-val-digits", "请输入整数。");
                        break;
                    case ValidationType.Email:
                        this.Attributes.Add("data-val-regex", "请输入正确格式的电子邮件。");
                        this.Attributes.Add("data-val-regex-pattern", "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})$");
                        break;
                    case ValidationType.URL:
                        this.Attributes.Add("data-val-regex", "请输入正确格式的URL。");
                        this.Attributes.Add("data-val-regex-pattern", "^((https|http|ftp|rtsp|mms)?://)?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|([0-9a-z_!~*\'()-]+.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$");
                        break;
                    case ValidationType.Regex:
                        this.Attributes.Add("data-val-regex", RegexMessage);
                        this.Attributes.Add("data-val-regex-pattern", RegexPattern);
                        break;
                }
            }

            //最大，最小值验证
            if (!string.IsNullOrEmpty(MaxVlaue) && !string.IsNullOrEmpty(MinValue))
            {
                needValidate = true;
                this.Attributes.Add("data-val-range", string.Format("请输入{0}到{1}之间的数值。", MinValue, MaxVlaue));
                this.Attributes.Add("data-val-range-max", MaxVlaue);
                this.Attributes.Add("data-val-range-min", MinValue);
            }
            else
            {
                //最小值验证
                if (!string.IsNullOrEmpty(MinValue))
                {
                    needValidate = true;
                    this.Attributes.Add("data-val-range", string.Format("请输入大于{0}的数值。", MinValue));
                    this.Attributes.Add("data-val-range-min", MinValue);
                }
                //最大值验证
                else if(!string.IsNullOrEmpty(MaxVlaue))
                {
                    needValidate = true;
                    this.Attributes.Add("data-val-range", string.Format("请输入小于{0}的数值。", MaxVlaue));
                    this.Attributes.Add("data-val-range-max", MaxVlaue);
                }
            }
            //验证长度范围
            if (MinValueLength>0 && MaxValueLength>0)
            {
                needValidate = true;
                this.Attributes.Add("data-val-length", string.Format("输入长度必须在{0}到{1}之间。", MinValueLength, MaxValueLength));
                this.Attributes.Add("data-val-length-max", MaxValueLength.Value.ToString());
                this.Attributes.Add("data-val-length-min", MinValueLength.Value.ToString());
            }
            else
            {
                //验证最小长度
                if (MinValueLength>0)
                {
                    needValidate = true;
                    this.Attributes.Add("data-val-length", string.Format("输入长度最少为{0}。", MinValueLength));

                    this.Attributes.Add("data-val-length-min", MinValueLength.Value.ToString());
                }
                //验证最大长度
                else if (MaxValueLength>0)
                {
                    needValidate = true;
                    this.Attributes.Add("data-val-length", string.Format("输入长度最长为{0}。", MaxValueLength));

                    this.Attributes.Add("data-val-length-max", MaxValueLength.Value.ToString());
                }
            }
            //验证二个值相等
            if (EqualToField.Length > 0)
            {
           
                needValidate = true;
                var equalID=  this.Parent.FindControl(EqualToField).UniqueID;
                this.Attributes.Add("data-val-equalto-other", equalID);
                this.Attributes.Add("data-val-equalto", string.Format("输入的值必须跟[{0}]一致。",EqualToFiledText));
                
            }


            if (needValidate)
            {
                this.Attributes.Add("data-val", "true"); 
            }
            base.OnPreRender(e);

        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {            
            base.Render(writer);

            if (Required)
            {
                writer.Write(RequrieText);
            }
        }
    }
}